package com.example.fujia.androidapp.activity;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.example.fujia.androidapp.R;
import com.example.fujia.androidapp.bean.User;
import com.example.fujia.androidapp.tools.Tools;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by fujia on 2016/6/13.
 */
public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText telephoneText;
    private EditText passwordText;
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        this.telephoneText = (EditText) findViewById(R.id.telephone);
        this.passwordText = (EditText) findViewById(R.id.password);
        this.button = (Button) findViewById(R.id.email_sign_in_button);

        this.button.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        if (!Tools.objectIsNullOrNot(this.telephoneText.getText().toString()) && !Tools.objectIsNullOrNot(this.passwordText.getText().toString())) {
            User.setTelephone(this.telephoneText.getText().toString());
            User.setPassword(this.passwordText.getText().toString());

            this.loginToService();
        }
    }

    private void loginToService(){
        RequestQueue queue = Volley.newRequestQueue(getApplicationContext());

        String url = "http://192.168.2.168:8080/json/login.action";


        Map<String, String> map = new HashMap<>();

        map.put("telephone", User.getTelephone());
        map.put("password", User.getPassword());

        JSONObject jsonObject = new JSONObject(map);

        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        Log.d("TAG", response.toString());
                        try {
                            boolean success = (boolean) response.get("success");
                           if (success){
                               int userId = (int) response.get("userId");

                               SharedPreferences sharedPreferences= getSharedPreferences("user", Activity.MODE_PRIVATE);
                               SharedPreferences.Editor editor = sharedPreferences.edit();
                               editor.putInt("userId", userId);
                               editor.commit();
                               finish();
                           }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("TAG", error.getMessage(), error);
            }
        });

        jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(600000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

        queue.add(jsonObjectRequest);
    }


}
